﻿using AutoMapper;
using B.S.Material.Domain.Scraps;
using B.S.Material.ErrorCode;
using B.S.Material.Wirth.Api.Applications.Command.ProductionWasteZCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Wirth.Api.Applications.Handler.ProductionWasteZHandler
{
    public class AddProductionWasteZHandler : IRequestHandler<AddProductionWasteZCommand, ApiResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddProductionWasteZHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<ApiResult<object>> Handle(AddProductionWasteZCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnum.添加成功;
            result.MAG = ApiEnum.添加成功.ToString();

            var model = mapper.Map<ProductionWasteZ>(request);
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "SCFL" + yeartime;
            model.DocumentNumber = ProductCodess1;
            model.IsDel = false;

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.Code = ApiEnum.添加成功;
                result.MAG = ApiEnum.添加成功.ToString();
            }
            else
            {
                result.Code = ApiEnum.添加失败;
                result.MAG = ApiEnum.添加失败.ToString();
            }
            return result;
        }
    }
}
